<template>
  <el-container class="home-container">
    <el-header class="title-header">
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <!-- active-text-color代表激活的文本颜色 -->
        <el-menu
          background-color="#545c64"
          text-color="#fff"
          active-text-color="#00BFFF"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          :router="true"
          :default-active="activePath"
        >
          <!-- 一级菜单 -->
          <el-submenu
            :index="index + ''"
            v-for="(item, index) in menuList"
            :key="index"
          >
            <template slot="title">
              <i :class="iconsObj[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <!-- 点击事件的saveNavState是带参数的，这个参数是在下面写点击函数的时候也在的，这个是什么，下面
            就是什么，只是名字不同，但是内容相同 -->
            <el-menu-item
              :index="'/' + child.path"
              v-for="(child, index) in item.children"
              :key="index"
              @click="saveNavState('/' + child.path)"
            >
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{ child.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <!-- Manin -->
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      menuList: [],
      iconsObj: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      isCollapse: false,
      // 被激活的链接地址
      activePath: ''
    }
  },
  created() {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    logout() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    async getMenuList() {
      const { data: res } = await this.$axios.get('menus')
      if (res.meta.status === 200) this.menuList = res.data
      // console.log(res)
    },
    // 点击按钮切换菜单的折叠与展开
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    saveNavState(activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
}
.title-header {
  background-color: #d3dce6;
}
.el-aside {
  background-color: #e9eef3;

  .el-menu {
    border-right: none;
  }
}
.iconfont {
  margin-right: 10px;
}
.toggle-button {
  height: 30px;
  background-color: #9c9c9c;
  color: #fff;
  font-size: 15px;
  line-height: 30px;
  text-align: center;
  // 字间距
  letter-spacing: 0.2em;
  // 设置鼠标样式
  cursor: pointer;
}
</style>
